/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.view;

import dreamwar.controller.ArmyJpaController;
import dreamwar.controller.GUIController;
import dreamwar.model.MyEntityManagerFactory;
import dreamwar.model.POJO.Army;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Berto_Romero
 */
public class ModifyArmyPanel extends javax.swing.JPanel {

    /**
     * Creates new form ModifyArmyJPanel
     */
    public ModifyArmyPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        armyBaysPanel = new javax.swing.JPanel();
        keypadPanel = new javax.swing.JPanel();
        commitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.columnWidths = new int[] {0};
        layout.rowHeights = new int[] {0, 5, 0};
        setLayout(layout);

        armyBaysPanel.setLayout(new java.awt.GridLayout(1, 0, 0, 10));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_END;
        gridBagConstraints.insets = new java.awt.Insets(25, 25, 0, 25);
        add(armyBaysPanel, gridBagConstraints);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 5, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        keypadPanel.setLayout(jPanel1Layout);

        commitButton.setText("Confirmar");
        commitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(commitButton, gridBagConstraints);

        cancelButton.setText("Cancelar");
        cancelButton.setMaximumSize(new java.awt.Dimension(79, 23));
        cancelButton.setMinimumSize(new java.awt.Dimension(79, 23));
        cancelButton.setPreferredSize(new java.awt.Dimension(79, 23));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        keypadPanel.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 25, 0);
        add(keypadPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        GUIController.getInstance().changeMainFrameCard(GUIController.ARMY_LIST_PANEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void commitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commitButtonActionPerformed
        updateArmy();
    }//GEN-LAST:event_commitButtonActionPerformed

    private void updateArmy() {
        for (ArmyBay armyBay : armyBayList) {
            armyBay.updateArmy();
        }
        ArmyJpaController armyJpaController = new ArmyJpaController(MyEntityManagerFactory.getInstance().getEntityManagerFactory());
        for (Army army : armyList) {
            try {
                armyJpaController.edit(army);
            } catch (Exception ex) {
                Logger.getLogger(ModifyArmyPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        GUIController.getInstance().changeMainFrameCard(GUIController.ARMY_LIST_PANEL);
    }
    
    public void addArmyBays(List<Army> armyList) {
        armyBaysPanel.removeAll();
        if (armyList.isEmpty()) return;
        this.armyList = armyList;
        armyBayList = new ArrayList<>();
        ((GridLayout) armyBaysPanel.getLayout()).setRows(armyList.size());
        for (Army army : armyList) {
            ArmyBay armyBay = new ArmyBay(army);
            armyBayList.add(armyBay);
            armyBaysPanel.add(armyBay);
        }
    }

    private List<Army> armyList;
    private List<ArmyBay> armyBayList;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel armyBaysPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton commitButton;
    private javax.swing.JPanel keypadPanel;
    // End of variables declaration//GEN-END:variables
}
